﻿using System.ServiceModel;

namespace Motar.ServiceModel
{
    
    /// <summary>
    /// 
    /// </summary>
    public static class ClientBaseExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TClient"></typeparam>
        /// <typeparam name="TChannel"></typeparam>
        /// <param name="client"></param>
        /// <returns></returns>
        public static TClient ApplyCookieBehavior<TClient, TChannel>(this TClient client) where TClient : ClientBase<TChannel> where TChannel : class
        {
            if (client.Endpoint.Behaviors.Find<CookieEndpointBehavior>() == null)
            {
                client.Endpoint.Behaviors.Add(new CookieEndpointBehavior());
            }

            return client;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TClient"></typeparam>
        /// <typeparam name="TChannel"></typeparam>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static TClient ApplyCookieBehavior<TClient, TChannel>(this TClient client, string key) where TClient : ClientBase<TChannel> where TChannel : class
        {
            if (client.Endpoint.Behaviors.Find<CookieEndpointBehavior>() == null)
            {
                client.Endpoint.Behaviors.Add(new CookieEndpointBehavior(key));
            }

            return client;
        }
    }
}
